AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")

function ENT:PostEntityPaste( ply, ent, createdents )
	self.ply = ply
	ply.broadsides = ply.broadsides+1
	self.price = 500
	
	if(tonumber(self.ply.money) < self.price or ply.broadsides>8) then 
	self.Repay = false
	self.Entity:Remove()
	return false
	else
	self.ply.money = self.ply.money-self.price
	MoneyHUDUpdate( self.ply )
	SavePlayerData( self.ply )
	end
end

function ENT:Initialize()
	self.Entity:SetModel("models/lk_models/lk_broadside.mdl") 
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
	self.Entity:SetTrigger( true )
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.Entity:SetUseType(SIMPLE_USE)
	
	self.Repay = true
	self.price = 500
	
	self.maxhp = GetHealth( self.Entity )
	self.hp = self.maxhp
	
	self.core = NULL
	
	self.minelevation = -15
	self.maxelevation = 15
	self.elevation = 0
	self.gunpitch = 0
	self.aimspeed = 0.5
	self.CanFire = CurTime()
	self.BarrelNum = 1
	
	local phy = self.Entity:GetPhysicsObject()
	if phy:IsValid() then phy:Wake() end
	
	self.Entity:ResetSequence(1)
	self.Entity:SetPlaybackRate(0)
	self.Entity:SetCycle(0.5)
end

function ENT:SpawnFunction( ply, tr )

	local price = 500
	
	if(tonumber(ply.money) < price or ply.broadsides>=8) then 
	return false
	else
	ply.money = ply.money-price
	MoneyHUDUpdate( ply )
	SavePlayerData( ply )
	ply.broadsides = ply.broadsides+1
	end
	
	local ent = ents.Create( "lk_broadside" )
	local SpawnPos = tr.HitPos + tr.HitNormal * 64
	ent:SetPos( SpawnPos )
	ent:SetAngles( Angle(0,tr.Normal:Angle().y,0) )
	ent:Activate()
	ent.ply = ply
	ent:Spawn()
	
	return ent
end

function ENT:Think()

if(!self.core:IsValid()) then return end

local AimDst = 2
if( self.core.driver!=nil and self.core.driver:IsValid() ) then
AimDst = (self.Entity:GetForward()-self.core.driver:GetEyeTrace().Normal):Length()
end

local pos = self.Entity:GetForward()*100
	if( self.core!=nil and self.core:GetClass()=="lk_core" and self.core.aimPos!=Vector(0,0,0) and AimDst<0.8) then
		pos = self.Entity:WorldToLocal( self.core.aimPos )
	end

local rad2deg = 180 / math.pi
	local len = pos:Length()
	local elevation = math.Clamp(rad2deg*math.asin(pos.z / len) ,self.minelevation,self.maxelevation)
	local pitchdelta = self.gunpitch-elevation
	if( math.abs(pitchdelta)>self.aimspeed ) then
	pitchdelta = pitchdelta/math.abs(pitchdelta)
	self.gunpitch = self.gunpitch - pitchdelta*self.aimspeed
	else
	self.gunpitch = elevation
	end

local range = self.maxelevation-self.minelevation
local deg = math.Clamp(self.gunpitch-self.minelevation,0,range)
local frame = deg/range

if( self.core.broadsidefire and self.CanFire<CurTime() and AimDst<0.8) then

	self.BarrelNum = -self.BarrelNum
	local ent = ents.Create( "lk_bullet" )
	local Pos = Vector(20,0,0)
	local Norm = Vector(1,0,0)
	local FirePos = Vector(150,0,14*self.BarrelNum)
	local Ang = Angle(-self.gunpitch,0,0)
	Norm:Rotate( Ang )
	FirePos:Rotate( Ang )
	local bulletPos = self.Entity:LocalToWorld(Pos + FirePos)
	ent:SetPos( bulletPos )
	ent:Activate()
	ent:Spawn()
	ent.core = self.core
	ent.Vel = self.Entity:LocalToWorld(Norm*100)-self.Entity:GetPos()
	ent:SetAngles( ent.Vel:Angle() )
	ent.gravity = 0.15
	ent.damage = 1000
	ent.dietime = CurTime()+0.5
	ent.ExpScale = 3
	
	self.Entity:EmitSound("sniper_fire.wav",140,80)
	
	local effectdata = EffectData()
	effectdata:SetOrigin( bulletPos )
	effectdata:SetNormal( ent.Vel:GetNormalized() )
	effectdata:SetScale( 2.5 )
	util.Effect( "lk_fireeffect" , effectdata )

	self.CanFire = CurTime()+3+math.random(1)
end

self.Entity:ResetSequence(1)
self.Entity:SetPlaybackRate(0)
self.Entity:SetCycle(math.Clamp(frame,0,1))

self.Entity:NextThink( CurTime() + 0.05 )
return true

end

function ENT:OnRemove()
	if(self.Repay==true) then
		if(self.core and self.core:IsValid()) then mult = self.core.hp/self.core.maxhp else mult = self.hp/self.maxhp end
		self.ply.money = self.ply.money + self.price*mult
		MoneyHUDUpdate( self.ply )
		SavePlayerData( self.ply )
	end
	self.ply.broadsides = self.ply.broadsides-1
end

